package com.hxhy.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hxhy.bean.Account;
import com.hxhy.bean.BElectronic;
import com.hxhy.bean.ElectronicException;
import com.hxhy.log.Logger;
import com.hxhy.service.InvoiceService;
import com.hxhy.util.Tools;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@CrossOrigin
public class InvoiceController {

	@Resource(name = "${version}")
	InvoiceService service;

	@Autowired
	private HttpServletRequest req;

	private Logger log = Logger.getLogger();

	/**
	 * @开具电子发票
	 * @param houseid
	 * @param kplx开票类型(1蓝票,2红票,3重发,4作废)
	 * @param paymenthousett购货方名称(蓝票)
	 * @param paymenthousesh购货方税号(蓝票)
	 * @param zfdz购货方地址(蓝票)
	 * @param gfdh购货方电话(蓝票)
	 * @param yx购货方邮箱(蓝票)
	 * @param gflx购货方企业类型(蓝票)
	 * @param gfyhzh购货方银行账号(蓝票)
	 * @param khh开户行(蓝票)
	 * @param bz备注(蓝票)
	 * @param fph发票号(红票)
	 * @param bbh版本号(红票)
	 * @param lsh流水号(红票)
	 * @param ids开票账目id(蓝票,红票)
	 * @param paymentaccountsubmit账目明细(蓝票)
	 * @param paymentaccountsubmit.areakind面积类型(蓝票)
	 * @param paymentaccountsubmit.annual费用年度(蓝票)
	 * @param paymentaccountsubmit.area面积大小
	 * @param paymentaccountsubmit.price单价
	 * @param paymentaccountsubmit.paysum开票金额(蓝票)
	 * @param paymentaccountsubmit.yhje优惠(蓝票)
	 * @param paymentaccountsubmit.znjwyj违约金(蓝票)
	 * @return json返回信息
	 * @return json.code状态码
	 * @return json.msg返回信息
	 * @return json.fph发票号
	 * @return json.bbh版本号
	 * @return json.url发票URL
	 * @return json.lsh发票请求流水号
	 */
	@RequestMapping(value = "/kpInvoice")
	@ResponseBody
	public String kpInvoice() {
		JSONObject js = null;
		String kplx = null;
		BElectronic e = null;

		// 参数处理
		js = JSONObject.fromObject(req.getParameter("jsondata"));
		if (js != null && js.containsKey("kplx")) {
			kplx = js.getString("kplx");
			e = new BElectronic();
			if (js.containsKey("sysusername") && !Tools.isNullOrEmpty(js.getString("sysusername")))
				e.setCreater(js.getString("sysusername"));
			if (js.containsKey("pjxx") && !Tools.isNullOrEmpty(js.getString("pjxx")))
				e.setInvoicemethod(js.getString("pjxx"));
			if (js.containsKey("houseid") && !Tools.isNullOrEmpty(js.getString("houseid")))
				e.setHouseid(js.getString("houseid"));
			if (js.containsKey("paymenthousett") && !Tools.isNullOrEmpty(js.getString("paymenthousett")))
				e.setGfmc(js.getString("paymenthousett"));
			if (js.containsKey("paymenthousesh") && !Tools.isNullOrEmpty(js.getString("paymenthousesh")))
				e.setNsrsbh(js.getString("paymenthousesh"));
			if (js.containsKey("gfdz") && !Tools.isNullOrEmpty(js.getString("gfdz")))
				e.setDz(js.getString("gfdz"));
			if (js.containsKey("gfdh") && !Tools.isNullOrEmpty(js.getString("gfdh")))
				e.setTel(js.getString("gfdh"));
			if (js.containsKey("yx") && !Tools.isNullOrEmpty(js.getString("yx")))
				e.setEmail(js.getString("yx"));
			if (js.containsKey("gflx") && !Tools.isNullOrEmpty(js.getString("gflx")))
				e.setCardno(js.getString("gflx"));
			if (js.containsKey("khh") && !Tools.isNullOrEmpty(js.getString("khh")))
				e.setKhh(js.getString("khh"));
			if (js.containsKey("gfyhzh") && !Tools.isNullOrEmpty(js.getString("gfyhzh")))
				e.setKhh(Tools.fillNull(e.getKhh()).concat(js.getString("gfyhzh")));
			if (js.containsKey("bz") && !Tools.isNullOrEmpty(js.getString("bz")))
				e.setRemark(js.getString("bz"));
			if (js.containsKey("fph") && !Tools.isNullOrEmpty(js.getString("fph")))
				e.setInvoicenum(js.getString("fph"));
			if (js.containsKey("bbh") && !Tools.isNullOrEmpty(js.getString("bbh")))
				e.setInvoiceversion(js.getString("bbh"));
			if (js.containsKey("lsh") && !Tools.isNullOrEmpty(js.getString("lsh")))
				e.setLsh(js.getString("lsh"));
			if (js.containsKey("ids") && !Tools.isNullOrEmpty(js.getString("ids")))
				e.setIds(js.getString("ids").split(","));
			if (js.containsKey("paymentaccountsubmit") && !Tools.isNullOrEmpty(js.get("paymentaccountsubmit"))) {
				JSONArray arr = js.getJSONArray("paymentaccountsubmit");
				List<Account> list = new ArrayList<Account>();
				if (arr != null && arr.size() > 0) {
					for (Object object : arr) {
						js = JSONObject.fromObject(object);
						if (js != null) {
							BigDecimal paysum = new BigDecimal(js.getString("paysum"));
							BigDecimal yhje = new BigDecimal(js.getString("yhje"));
							BigDecimal znj = new BigDecimal(js.getString("wyj"));
							BigDecimal jfje = paysum.subtract(znj);
							if(-1 == BigDecimal.ZERO.compareTo(jfje)){
								Account ac = new Account();
								if (js.containsKey("areakind") && !Tools.isNullOrEmpty(js.getString("areakind")))
									ac.setAreakind(js.getString("areakind"));
								if (js.containsKey("annual") && !Tools.isNullOrEmpty(js.getString("annual")))
									ac.setAnnual(js.getString("annual"));
								if (js.containsKey("paysum") && !Tools.isNullOrEmpty(js.getString("paysum")))
									ac.setReceived(jfje);
								if (js.containsKey("area") && !Tools.isNullOrEmpty(js.getString("paysum")))
									ac.setArea(new BigDecimal(js.getString("area")));
								if (js.containsKey("price") && !Tools.isNullOrEmpty(js.getString("price")))
									ac.setPrice(new BigDecimal(js.getString("price")));
								ac.setActflag1("1");
								list.add(ac);
							}
							if(0 != BigDecimal.ZERO.compareTo(yhje)){
								Account ac = new Account();
								if (js.containsKey("areakind") && !Tools.isNullOrEmpty(js.getString("areakind")))
									ac.setAreakind(js.getString("areakind"));
								if (js.containsKey("annual") && !Tools.isNullOrEmpty(js.getString("annual")))
									ac.setAnnual(js.getString("annual"));
								if (js.containsKey("paysum") && !Tools.isNullOrEmpty(js.getString("paysum")))
									ac.setReceived(yhje);
								if (js.containsKey("area") && !Tools.isNullOrEmpty(js.getString("paysum")))
									ac.setArea(new BigDecimal(js.getString("area")));
								if (js.containsKey("price") && !Tools.isNullOrEmpty(js.getString("price")))
									ac.setPrice(new BigDecimal(js.getString("price")));
								ac.setActflag1("3");
								list.add(ac);
							}
							if(-1 == BigDecimal.ZERO.compareTo(znj)){
								Account ac = new Account();
								if (js.containsKey("areakind") && !Tools.isNullOrEmpty(js.getString("areakind")))
									ac.setAreakind(js.getString("areakind"));
								if (js.containsKey("annual") && !Tools.isNullOrEmpty(js.getString("annual")))
									ac.setAnnual(js.getString("annual"));
								if (js.containsKey("paysum") && !Tools.isNullOrEmpty(js.getString("paysum")))
									ac.setReceived(znj);
								if (js.containsKey("area") && !Tools.isNullOrEmpty(js.getString("paysum")))
									ac.setArea(new BigDecimal(js.getString("area")));
								if (js.containsKey("price") && !Tools.isNullOrEmpty(js.getString("price")))
									ac.setPrice(new BigDecimal(js.getString("price")));
								ac.setActflag1("5");
								list.add(ac);
							}
						}
					}
				}
				e.setActs(list);
			}

		} else {
			kplx = req.getParameter("kplx");
			if (Tools.isNullOrEmpty(kplx)) {
				js = new JSONObject();
				js.put("code", "9999");
				js.put("msg", "开票类型必须传入");
			} else {
				e = new BElectronic();
				e.setInvoicenum(req.getParameter("fph"));
				e.setInvoiceversion(req.getParameter("bbh"));
				e.setInvoicemethod(req.getParameter("pjxx"));
			}
		}

		// 接口调用
		if (e != null) {
			try {
				if ("1".equals(kplx)) {
					e = service.kpInvoice(e);
				} else if ("2".equals(kplx)) {
					e = service.hpInvoice(e);
				} else if ("3".equals(kplx)) {
					e = service.sendInvoice(e);
				} else if ("4".equals(kplx)) {
					e = service.zfInvoice(e);
				}
			} catch (ElectronicException exp) {
				e = exp.getInfo();
			}

			js = new JSONObject();
			js.put("resurl", "/module/saveModuleInformation");
			js.put("code", e.getReturncode());
			js.put("msg", e.getReturnstate());
			if (!Tools.isNullOrEmpty(e.getHouseid()))
				js.put("houseid", e.getHouseid());
			if (!Tools.isNullOrEmpty(e.getTel()))
				js.put("tel", e.getTel());
			if (!Tools.isNullOrEmpty(e.getMoney()))
				js.put("money", e.getMoney());
			if (!Tools.isNullOrEmpty(e.getCardno()))
				js.put("cardno", e.getCardno());
			if (!Tools.isNullOrEmpty(e.getEmail()))
				js.put("email", e.getEmail());
			if (!Tools.isNullOrEmpty(e.getGfmc()))
				js.put("gfmc", e.getGfmc());
			if (!Tools.isNullOrEmpty(e.getNsrsbh()))
				js.put("nsrsbh", e.getNsrsbh());
			if (!Tools.isNullOrEmpty(e.getKhh()))
				js.put("khh", e.getKhh());
			if (!Tools.isNullOrEmpty(e.getDz()))
				js.put("dz", e.getDz());
			if (!Tools.isNullOrEmpty(e.getInvoicemethod()))
				js.put("invoicemethod", e.getInvoicemethod());
			if (!Tools.isNullOrEmpty(e.getInvoicekind()))
				js.put("invoicekind", e.getInvoicekind());
			if (!Tools.isNullOrEmpty(e.getLsh()))
				js.put("lsh", e.getLsh());
			if (!Tools.isNullOrEmpty(e.getInvoicenum()))
				js.put("fph", e.getInvoicenum());
			if (!Tools.isNullOrEmpty(e.getInvoiceversion()))
				js.put("bbh", e.getInvoiceversion());
			if (!Tools.isNullOrEmpty(e.getPdfpath()))
				js.put("url", e.getPdfpath());
			if (!Tools.isNullOrEmpty(e.getReverselsh()))
				js.put("reverselsh", e.getReverselsh());
			if (!Tools.isNullOrEmpty(e.getPdfbytes()))
				js.put("pdfbytes", e.getPdfbytes());
			if (!Tools.isNullOrEmpty(e.getYzm()))
				js.put("yzm", e.getYzm());
			if (!Tools.isNullOrEmpty(e.getSkr()))
				js.put("skr", e.getSkr());
			if (!Tools.isNullOrEmpty(e.getKpr()))
				js.put("kpr", e.getKpr());
			if (!Tools.isNullOrEmpty(e.getFhr()))
				js.put("fhr", e.getFhr());
			if (!Tools.isNullOrEmpty(e.getRemark()))
				js.put("remark", e.getRemark());
			if (!Tools.isNullOrEmpty(e.getFlag()))
				js.put("flag", e.getFlag());
		}
		log.info("实时开票返回信息:" + js.toString());

		return js.toString();
	}
}
